const cloud = require('wx-server-sdk')
cloud.init({
    env: 'zbdc-6gavbhj633c62e27'
})
const db = cloud.database()
const _ = db.command

exports.main = async (event, context) => {
    const wxContext = cloud.getWXContext()
    // console.log(event.type, event.id, event.openid)
    if (event.type == 'rvewList') {
        try {
            return await db.collection('reviewList').where({
                _openid: wxContext.OPENID,
                year: event.year,
                mon: _.in(event.mon),
                day: _.in(event.day)
            }).remove()
        } catch (e) {
            console.error(e)
        }
    } else if (event.type == 'rvmStar') {
        return await db.collection("userData")
            .where({
                openid: wxContext.OPENID,
            })
            .update({
                data: {
                    starData: _.pull({
                        _id: event.id
                    })
                }
            })
    } else if (event.type == 'rvmnote') {

        return await db.collection("userData")
            .where({
                openid: wxContext.OPENID,
            })
            .update({
                data: {
                    noteBook: _.pull({
                        _id: event.id
                    })
                }
            })
    }
}